# mkenv.mk -- global Makefile environment
# 
# Copyright(c) 2021 Cai_XL <Cai_XL@outlook.com>
# bilibili : https://space.bilibili.com/54910927
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


# where to output
ifneq ($(origin BUILD_DIR),command line)
BUILD_DIR = build
endif

# quite mode
ifneq ($(origin V),command line)
V = 0
endif

ifeq ($(V),0)
Q = @
endif

# cross toolchain selection
ifneq ($(origin CROSS_COMPILE),command line)
CROSS_COMPILE = arm-none-eabi-
endif

# cross toolchain
CROSS_CC 	= $(CROSS_COMPILE)gcc
CROSS_CPP 	= $(CROSS_COMPILE)g++
CROSS_CP 	= $(CROSS_COMPILE)objcopy
CROSS_DP  	= $(CROSS_COMPILE)objdump
CROSS_SZ 	= $(CROSS_COMPILE)size
CROSS_LD 	= $(CROSS_COMPILE)ld
CROSS_ASM = $(CROSS_CC) -x assembler-with-cpp
CROSS_HEX = $(CROSS_CP) -O ihex
CROSS_BIN = $(CROSS_CP) -O binary -S

# host compiler always use gcc 
HOST_CC = gcc

# parameters for toolchain
CPU = arm926ej-s
ARCH = armv5te
DEF = -D__ARM926EJS__ -D__ARM32_ARCH__=5
CFLAGS = -mtune=$(CPU) -march=$(ARCH) -mthumb
CFLAGS += -Wall -fdata-sections -ffunction-sections -ffreestanding -flto -g
CFLAGS += $(DEF)
ASMFLAGS = -g -ggdb -Wall -march=armv5te -mfloat-abi=soft

LDFLAGS = -mtune=$(CPU) -march=$(ARCH) -lgcc 
LDFLAGS += -nostdlib  -Wl,--gc-sections

.PHONY:all clean 
all: $(BUILD_DIR)

$(BUILD_DIR):
	$(Q) mkdir $@

clean: 
	$(Q) echo cleaning...
	$(Q) rm -rf $(BUILD_DIR)/*

